package com.cdtye.itps.cms.controller.equipment;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdtye.common.authcore.model.UserSso;
import com.cdtye.common.authcore.util.AuthHelper;
import com.cdtye.common.webcore.base.BaseController;
import com.cdtye.common.webcore.util.DateUtil;
import com.cdtye.common.webcore.web.AjaxJson;
import com.cdtye.itps.cms.dao.enums.EquipmentEnum;
import com.cdtye.itps.cms.dao.equipment.entity.EquipmentProducer;
import com.cdtye.itps.cms.dao.equipment.vo.EquipmentProducerVo;
import com.cdtye.itps.cms.service.equipment.EquipmentProducerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Api("设备厂家 前端控制器")
@RestController
@RequestMapping("/equipmentProducer")
public class EquipmentProducerController extends BaseController {
    @Autowired
    private EquipmentProducerService modelService;


    @ApiOperation(value = "保存或更新", notes = "返回对象最新数据")
    @PostMapping(value = "/saveOrUpdate")
    public AjaxJson<EquipmentProducer> saveOrUpdate(@RequestBody EquipmentProducer model){
        UserSso loginUser = AuthHelper.loginUser();
        Date nowDate = DateUtil.getDateTime();

        model.setCreateBy(loginUser.getId());
        model.setCreateDatetime(nowDate);
        model.setUpdateDatetime(nowDate);
        model.setDelFlig("Y");

        model.setUpdateBy(loginUser.getId());
        model.setUpdateDatetime(nowDate);
        modelService.saveOrUpdate(model);
        return success(model);
    }

    @ApiOperation(value = "按ID删除")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "String")
    @DeleteMapping(value = "/{id}")
    public AjaxJson deleteById(@PathVariable("id") String id) {
        if(StringUtils.isBlank(id)){
            return error("未获取到参数");
        }
        EquipmentProducer equipmentProducer = modelService.getById(id);
        equipmentProducer.setDelFlig(EquipmentEnum.NO.getText());
        modelService.saveOrUpdate(equipmentProducer);
        return success();
    }


    @ApiOperation(value = "按ID查询")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "String")
    @GetMapping(value = "/{id}")
    public AjaxJson getById(@PathVariable("id") String id) {
        if(StringUtils.isBlank(id)){
            return error("未获取到参数");
        }
        EquipmentProducer byId = modelService.getById(id);
        return success(byId);
    }

    @ApiOperation(value = "列表查询")
    @GetMapping(value = "/list")
    public AjaxJson<List<Map<String,Object>>> list(EquipmentProducerVo vo){
        List<Map<String,Object>> result = modelService.list(vo);
        return success(result);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/listPage")
    public AjaxJson<IPage<Map<String,Object>>> listPage(EquipmentProducerVo vo){
        IPage<Map<String,Object>> result = modelService.listPage(vo);
        return success(result);
    }

}
